import json

c_dict = {}


# absoluteIndex,index,name,字段,来源,编写注意,示例
def add_to_dict(absolute_index, index, name, field, source, note, example):
    if name not in c_dict:
        c_dict[name] = {
            "absolute_index": [],
            "index": index,
            "name": name,
            "field": [],
            "source": [],
            "note": [],
            "example": []

        }

    c_dict[name]["absolute_index"].append(absolute_index)
    c_dict[name]["field"].append(field)
    c_dict[name]["source"].append(source)
    c_dict[name]["note"].append(note)
    c_dict[name]["example"].append(example)


def csv_transform(lines: list):
    title = lines[0]
    context = lines[1:]
    for line in context:
        line = line.split(',')
        add_to_dict(
            line[0],
            line[1],
            line[2],
            line[3],
            line[4],
            line[5],
            line[6],
        )


def main():
    csv_data = []
    with open("社团文件汇总.csv", 'r', encoding='utf-8') as f:
        for line in f.readlines():
            csv_data.append(line.strip())

        # csv_data = f.readlines()
    csv_transform(lines=csv_data)
    print(c_dict)
    json_obj = []
    for key, value in c_dict.items():
        json_obj.append(value)

    with open("json_out.json", "w+", encoding='utf-8') as f:
        json.dump(json_obj, f, ensure_ascii=False, indent=1)


if __name__ == '__main__':
    print("社团文件生成器V0.1")
    main()
